//
//  FeedBackViewController.m
//  SoupApp
//
//  Created by pro1 on 2018/11/20.
//  Copyright © 2018 pro2. All rights reserved.
//

#import "FeedBackViewController.h"
#import "FeedBackTableViewCell.h"
#import "FeedBackModel.h"
#import "FeedBackBottomView.h"
//#import "DatabaseManager.h"

@interface FeedBackViewController ()<UITableViewDelegate,UITableViewDataSource,FeedBackBottomViewDelegate>

@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray<FeedBackModel *> *list;
@property (strong,nonatomic) FeedBackBottomView *bottomView;

@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = RGB(245, 245, 245);
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(NavigationContentTop);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(-(PreferredValueForNotchedDevice(39, 0) + 47));
    }];
    self.naviTitle.text = @"意见反馈";
    _bottomView = [[FeedBackBottomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - (PreferredValueForNotchedDevice(39, 0) + 47), SCREEN_WIDTH, (PreferredValueForNotchedDevice(39, 0) + 47))];
    _bottomView.delegate = self;
    [self.view addSubview:_bottomView];
    
    [self getdata];
}

- (void)getdata{
    weakify(self);
    [self getUserCommentListSuccess:^(NSMutableArray * _Nonnull listArr) {
        strongify(self);
        [self setList:listArr];
        [self.tableView reloadData];
    } failed:^(NSString * _Nonnull error) {
    }];
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    if (_tableView) {
//        [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.list.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
//    }
//}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = RGB(245, 245, 245);
        [_tableView registerClass:[FeedBackTableViewCell class] forCellReuseIdentifier:NSStringFromClass([FeedBackTableViewCell class])];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.list.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FeedBackModel *model = self.list[indexPath.row];
    NSLog(@"%f",model.cellHeight);
    return model.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FeedBackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FeedBackTableViewCell class])];
    cell.model = self.list[indexPath.row];
    return cell;
}

- (void)bottomFrameChanged:(CGFloat)height{
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-height);
    }];
    if (self.list && self.list.count > 0) {
        [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.list.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}

- (void)userSendTextWith:(NSString *)text{
    weakify(self);
    [self addCommentWithContent:text Success:^{
        strongify(self);
        FeedBackModel *model = [[FeedBackModel alloc] init];
        model.content = text;
        model.is_user = 1;
        [model toCalculateCellHeight];
        //刷新页面
        [self.list addObject:model];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.list.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.list.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        //重置底部View的高度
        [self.bottomView reloadBootomView];
        
    } failed:^(NSString * _Nonnull error) {
    }];
}


@end
